<html>
<head><meta charset="utf-8"><title>Attached threads whose parent exits · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Attached.20threads.20whose.20parent.20exits.html">Attached threads whose parent exits</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="194607822"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Attached%20threads%20whose%20parent%20exits/near/194607822" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Attached.20threads.20whose.20parent.20exits.html#194607822">(Apr 19 2020 at 17:01)</a>:</h4>
<p>Does anyone know if POSIX (or other concurrency APIs) say anything about what happens when a thread is attached to a parent thread, and the parent thread exits first? I tried to find out from the docs but didn't find a clear statement. Maybe I was looking in the wrong place.<br>
(See <a href="https://github.com/rust-lang/miri/pull/1284#discussion_r400812567" title="https://github.com/rust-lang/miri/pull/1284#discussion_r400812567">here for the context for this question</a>.)<br>
Cc <span class="user-mention" data-user-id="133247">@bjorn3</span> who originally mentioned this.</p>



<a name="194608348"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Attached%20threads%20whose%20parent%20exits/near/194608348" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Attached.20threads.20whose.20parent.20exits.html#194608348">(Apr 19 2020 at 17:12)</a>:</h4>
<p>I may have remembered it wrong. At least exiting from the main thread kills the other threads though.</p>



<a name="194608516"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Attached%20threads%20whose%20parent%20exits/near/194608516" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Attached.20threads.20whose.20parent.20exits.html#194608516">(Apr 19 2020 at 17:16)</a>:</h4>
<p>Except when calling <code>pthread_exit</code>, in which case the other threads continue.</p>



<a name="194608571"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Attached%20threads%20whose%20parent%20exits/near/194608571" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Attached.20threads.20whose.20parent.20exits.html#194608571">(Apr 19 2020 at 17:17)</a>:</h4>
<p><a href="https://computing.llnl.gov/tutorials/pthreads/#CreatingThreads" title="https://computing.llnl.gov/tutorials/pthreads/#CreatingThreads">https://computing.llnl.gov/tutorials/pthreads/#CreatingThreads</a> Link: <a href="https://computing.llnl.gov/tutorials/pthreads/#CreatingThreads" title="https://computing.llnl.gov/tutorials/pthreads/#CreatingThreads">https://computing.llnl.gov/tutorials/pthreads/#CreatingThreads</a></p>



<a name="194608850"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Attached%20threads%20whose%20parent%20exits/near/194608850" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Attached.20threads.20whose.20parent.20exits.html#194608850">(Apr 19 2020 at 17:26)</a>:</h4>
<p>lucky enough <code>pthread_exit</code> is not part of this proposal :D</p>



<a name="194609048"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Attached%20threads%20whose%20parent%20exits/near/194609048" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Attached.20threads.20whose.20parent.20exits.html#194609048">(Apr 19 2020 at 17:30)</a>:</h4>
<p>also Cc <span class="user-mention" data-user-id="116109">@Vytautas Astrauskas</span></p>



<a name="194609060"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Attached%20threads%20whose%20parent%20exits/near/194609060" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Attached.20threads.20whose.20parent.20exits.html#194609060">(Apr 19 2020 at 17:30)</a>:</h4>
<p>hm so these "resources" that need to be freed, does that mean if we do not detach a thread and do not join it there is a leak? could we make miri detect that leak?</p>



<a name="194609078"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Attached%20threads%20whose%20parent%20exits/near/194609078" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Attached.20threads.20whose.20parent.20exits.html#194609078">(Apr 19 2020 at 17:31)</a>:</h4>
<p>also is the parent thread relationship used for <em>anything</em>? doesnt seem like it, any thread can join any other thread I think...</p>



<a name="194609086"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Attached%20threads%20whose%20parent%20exits/near/194609086" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Attached.20threads.20whose.20parent.20exits.html#194609086">(Apr 19 2020 at 17:31)</a>:</h4>
<p>however, " It is a logical error to attempt multiple joins on the same thread. "</p>



<a name="194609089"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Attached%20threads%20whose%20parent%20exits/near/194609089" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Attached.20threads.20whose.20parent.20exits.html#194609089">(Apr 19 2020 at 17:31)</a>:</h4>
<p>we should catch that as UB</p>



<a name="194611594"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Attached%20threads%20whose%20parent%20exits/near/194611594" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Attached.20threads.20whose.20parent.20exits.html#194611594">(Apr 19 2020 at 18:30)</a>:</h4>
<p>A <code>pthread_t</code> join handle isn't "owned" by any particular thread in pthreads. It can be freely sent from one thread to another.</p>



<a name="194611614"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Attached%20threads%20whose%20parent%20exits/near/194611614" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Attached.20threads.20whose.20parent.20exits.html#194611614">(Apr 19 2020 at 18:31)</a>:</h4>
<p>Calling <code>pthread_detach</code> or <code>pthread_join</code> effectively "consumes" the <code>pthread_t</code>, it becomes invalid afterwards.</p>



<a name="194611660"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Attached%20threads%20whose%20parent%20exits/near/194611660" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Attached.20threads.20whose.20parent.20exits.html#194611660">(Apr 19 2020 at 18:32)</a>:</h4>
<p>Exiting from <code>main()</code> will kill all other threads as if <code>exit()</code> was called. However if the main thread calls <code>pthread_exit</code> then other threads continue running.</p>



<a name="194641388"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Attached%20threads%20whose%20parent%20exits/near/194641388" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Attached.20threads.20whose.20parent.20exits.html#194641388">(Apr 20 2020 at 07:39)</a>:</h4>
<p><span class="user-mention silent" data-user-id="143274">Amanieu</span> <a href="#narrow/stream/122651-general/topic/Attached.20threads.20whose.20parent.20exits/near/194611594" title="#narrow/stream/122651-general/topic/Attached.20threads.20whose.20parent.20exits/near/194611594">said</a>:</p>
<blockquote>
<p>A <code>pthread_t</code> join handle isn't "owned" by any particular thread in pthreads. It can be freely sent from one thread to another.</p>
</blockquote>
<p>okay so I take this to mean that the parent thread relationship is entirely irrelevant</p>



<a name="194641419"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Attached%20threads%20whose%20parent%20exits/near/194641419" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Attached.20threads.20whose.20parent.20exits.html#194641419">(Apr 20 2020 at 07:39)</a>:</h4>
<p><span class="user-mention silent" data-user-id="143274">Amanieu</span> <a href="#narrow/stream/122651-general/topic/Attached.20threads.20whose.20parent.20exits/near/194611660" title="#narrow/stream/122651-general/topic/Attached.20threads.20whose.20parent.20exits/near/194611660">said</a>:</p>
<blockquote>
<p>Exiting from <code>main()</code> will kill all other threads as if <code>exit()</code> was called. However if the main thread calls <code>pthread_exit</code> then other threads continue running.</p>
</blockquote>
<p>what does this mean for TLS dtors? do they get run for the "killed" threads?</p>



<a name="194662022"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Attached%20threads%20whose%20parent%20exits/near/194662022" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Attached.20threads.20whose.20parent.20exits.html#194662022">(Apr 20 2020 at 11:29)</a>:</h4>
<p>No, I don't think so.</p>



<a name="194662057"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Attached%20threads%20whose%20parent%20exits/near/194662057" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Attached.20threads.20whose.20parent.20exits.html#194662057">(Apr 20 2020 at 11:29)</a>:</h4>
<p>Actually, I am 100% sure they are not executed on the killed threads.</p>



<a name="194662077"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Attached%20threads%20whose%20parent%20exits/near/194662077" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Attached.20threads.20whose.20parent.20exits.html#194662077">(Apr 20 2020 at 11:29)</a>:</h4>
<p>Whether they are executed on the main thread is a bit blurry: <a href="https://github.com/rust-lang/rust/issues/28129" title="https://github.com/rust-lang/rust/issues/28129">#28129</a></p>



<a name="194703367"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Attached%20threads%20whose%20parent%20exits/near/194703367" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Attached.20threads.20whose.20parent.20exits.html#194703367">(Apr 20 2020 at 16:52)</a>:</h4>
<p><span class="user-mention silent" data-user-id="143274">Amanieu</span> <a href="#narrow/stream/122651-general/topic/Attached.20threads.20whose.20parent.20exits/near/194662077" title="#narrow/stream/122651-general/topic/Attached.20threads.20whose.20parent.20exits/near/194662077">said</a>:</p>
<blockquote>
<p>Whether they are executed on the main thread is a bit blurry: <a href="https://github.com/rust-lang/rust/issues/28129" title="https://github.com/rust-lang/rust/issues/28129">#28129</a></p>
</blockquote>
<p>ah, that's interesting... miri has run them so far.  I guess that's the conservative choice here; if there are leaks in the real execution because TLS dtors dont run they dont matter as the process is quitting anyway. and if the TLS dtors are wrong/UB, this way we catch that.</p>



<a name="194703427"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Attached%20threads%20whose%20parent%20exits/near/194703427" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Attached.20threads.20whose.20parent.20exits.html#194703427">(Apr 20 2020 at 16:53)</a>:</h4>
<p>so I guess we should run TLS dtors for joined threads, the main thread... and I suppose detached threads when they are done?</p>



<a name="194703671"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Attached%20threads%20whose%20parent%20exits/near/194703671" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Attached.20threads.20whose.20parent.20exits.html#194703671">(Apr 20 2020 at 16:55)</a>:</h4>
<p>We shouldn't run them for the main thread when using <code>pthread_exit</code> from the main thread, as the program can continue executing.</p>



<a name="194704904"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Attached%20threads%20whose%20parent%20exits/near/194704904" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Attached.20threads.20whose.20parent.20exits.html#194704904">(Apr 20 2020 at 17:05)</a>:</h4>
<p>we don't support <code>pthread_exit</code> at all</p>



<a name="194705114"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Attached%20threads%20whose%20parent%20exits/near/194705114" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Attached.20threads.20whose.20parent.20exits.html#194705114">(Apr 20 2020 at 17:06)</a>:</h4>
<p>Forgot that</p>



<a name="194705312"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Attached%20threads%20whose%20parent%20exits/near/194705312" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Attached.20threads.20whose.20parent.20exits.html#194705312">(Apr 20 2020 at 17:08)</a>:</h4>
<p>detached threads are interesting though</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>